package com.int_tech.newTimeTask;

import com.int_tech.car.dao.CarDao;
import com.int_tech.control.dao.SocketLogSendDao;
import com.int_tech.control.entity.SocketLogSend;
import com.int_tech.mession.dao.MessionDao;
import com.int_tech.mession.entity.Mession;
import com.int_tech.messionLog.dao.MessionLogShowDao;
import com.int_tech.messionLog.entity.MessionLogShow;
import com.int_tech.station.dao.StationDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created by liubch on 2018/5/27.
 */
@Component
public class CMessionLogTimeTask {

    @Autowired
    private CarDao carDao;

    @Autowired
    private StationDao stationDao;

    @Autowired
    private MessionDao messionDao;

    @Autowired
    private SocketLogSendDao socketLogSendDao;

    @Autowired
    private MessionLogShowDao messionLogShowDao;


    //绘制路线图
    @Scheduled(cron="0/10 * * * * ?")
    public void checkMessionLog(){

        //查找已经开关门完成并结束的订单
        List<Mession> messionList = messionDao.findByIsOverAndCheckStatus(1,2);


        //一个一个来，如果任务单为空，什么都不做
        if(messionList == null || messionList.size() == 0)
        {

        }
        else
        {
            //获取第一个
//            Mession mession = messionList.get(0);

            for(Mession mession : messionList)
            {
                if(mession.getStartOpenDoorTime() == null || mession.getEndOpenDoorTime() == null)
                {
                    //任务异常，不做处理
                    mession.setCheckStatus(-3);
                    messionDao.save(mession);
                }
                else
                {
                    mession.setCheckStatus(3);
                    messionDao.save(mession);

                    try
                    {
                        //开始时间
                        Date startTime = mession.getStartOpenDoorTime();

                        //结束时间
                        Date endTime = mession.getEndCloseDoorTime();

                        //获取开关门时间内，此车辆的所有坐标点，插入数据库
                        //车牌号
                        String carNumber = mession.getCarNumber();

                        List<SocketLogSend> socketLogSendList = socketLogSendDao.findByCarNumberAndStartOpenDoorTimeAndEndCloseDoorTime(carNumber,startTime,endTime);


                        //遍历，将A0,A2,A4存储在log中
                        for (SocketLogSend socketLogSend : socketLogSendList)
                        {
                            if("A0".equals(socketLogSend.getCode())||"A2".equals(socketLogSend.getCode())||"A4".equals(socketLogSend.getCode()))
                            {
                                MessionLogShow messionLogShow = new MessionLogShow();

                                String logData = socketLogSend.getDataString();

                                String[] logs = logData.split(",");

                                //经纬度
                                String slng = logs[7];
                                String slat = logs[8];

                                //时间
                                String rtcTime = logs[6];
                                String gepTime = logs[9];


                                messionLogShow.setCarNumber(mession.getCarNumber());
                                messionLogShow.setRtcTime(rtcTime);
                                messionLogShow.setGpsIsFail(0);
                                messionLogShow.setGepUploadTime(gepTime);
                                messionLogShow.setLat(slat);
                                messionLogShow.setLng(slng);
                                messionLogShow.setMessionNumber(mession.getMessionNumber());
                                messionLogShow.setCreateTime(socketLogSend.getCreateTime());
                                messionLogShow.setDoorMessage("close");
                                messionLogShow.setMessionCount(String.valueOf(mession.getMessionCount()));

                                messionLogShowDao.save(messionLogShow);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();

                        mession.setCheckStatus(-3);
                        messionDao.save(mession);
                    }
                }
            }
        }
    }

}
